﻿package com.jfdesgagne.jfdesgagne09.core.model {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.xml.XMLDocument;
	import com.jfdesgagne.util.XMLFileParser;
	
	public dynamic class ConfigurationModel extends EventDispatcher {
		private var _urlLoader:URLLoader;
		private var _urlRequest:URLRequest;
		public var xml:XMLDocument;
		
		public function ConfigurationModel(_params:Object) {
			this._setParams(_params);
			this._urlRequest = new URLRequest(this.http_address+"system/config/config.xml")
			this._urlLoader = new URLLoader(this._urlRequest);
			this._urlLoader.addEventListener(Event.COMPLETE, this._configLoadCompletedHandler);
			this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this._configErrorHandler);
		}
		
		private function _configErrorHandler($event:IOErrorEvent):void {
			trace("EEEEEEEEEEEEEEEERRROROROROROR !!!!");
			this._urlLoader.load(this._urlRequest);
		}
		
		private function _setParams(_params:Object):void {
			for(var element:String in _params) {
				this.element = _params[element];
			}
			
			if(this.http_address == undefined) this.http_address = "http://localhost/jfdesgagne.com/";
		}
		
		private function _configLoadCompletedHandler(event:Event):void {
			this.xml = new XMLDocument(event.target.data);
			var xmlFileParser:XMLFileParser = new XMLFileParser();
				xmlFileParser.parseXML(this.xml, this);
				
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}